/*     */ package com.zimbra.cs.mailbox.calendar;
/*     */ 
/*     */ import com.zimbra.common.auth.ZAuthToken;
/*     */ import com.zimbra.common.service.ServiceException;
/*     */ import com.zimbra.common.soap.AdminConstants;
/*     */ import com.zimbra.common.soap.Element;
/*     */ import com.zimbra.common.soap.Element.XMLElement;
/*     */ import com.zimbra.common.soap.SoapFaultException;
/*     */ import com.zimbra.common.soap.SoapTransport;
/*     */ import com.zimbra.common.util.ByteUtil;
/*     */ import com.zimbra.common.util.CliUtil;
/*     */ import com.zimbra.cs.mailbox.calendar.tzfixup.XmlFixupRules;
/*     */ import com.zimbra.cs.util.SoapCLI;
/*     */ import java.io.File;
/*     */ import java.io.IOException;
/*     */ import java.io.PrintStream;
/*     */ import java.text.SimpleDateFormat;
/*     */ import java.util.Date;
/*     */ import org.apache.commons.cli.CommandLine;
/*     */ import org.apache.commons.cli.Option;
/*     */ import org.apache.commons.cli.Options;
/*     */ import org.apache.commons.cli.ParseException;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class FixCalendarTZUtil
/*     */   extends SoapCLI
/*     */ {
/*     */   protected static final String O_RULEFILE = "rulefile";
/*     */   protected static final String O_ACCOUNT = "a";
/*     */   protected static final String O_AFTER = "after";
/*     */   protected static final String O_SYNC = "sync";
/*     */   
/*     */   protected void setupCommandLineOptions()
/*     */   {
/*  48 */     super.setupCommandLineOptions();
/*  49 */     Options options = getOptions();
/*  50 */     Option rulefileOpt = new Option(null, "rulefile", true, "xml file containing fixup rules");
/*  51 */     rulefileOpt.setRequired(true);
/*  52 */     options.addOption(rulefileOpt);
/*  53 */     Option accountOpt = new Option("a", "account", true, "account email addresses seperated by white space or \"all\" for all accounts");
/*     */     
/*  55 */     accountOpt.setRequired(true);
/*  56 */     accountOpt.setArgs(-2);
/*  57 */     options.addOption(accountOpt);
/*  58 */     options.addOption(new Option(null, "after", true, "fixup calendar items after this time; defaults to beginning of 2007"));
/*     */     
/*  60 */     options.addOption(new Option(null, "sync", false, "run synchronously; default is asynchronous"));
/*     */     
/*  62 */     options.addOption(SoapCLI.OPT_AUTHTOKEN);
/*  63 */     options.addOption(SoapCLI.OPT_AUTHTOKENFILE);
/*     */   }
/*     */   
/*     */   protected String getCommandUsage()
/*     */   {
/*  68 */     return "zmtzupdate --rulefile <rule file> -a <account(s)> [options]";
/*     */   }
/*     */   
/*     */   public FixCalendarTZUtil() throws ServiceException
/*     */   {
/*  73 */     setupCommandLineOptions();
/*     */   }
/*     */   
/*     */   public static void main(String[] args) {
/*  77 */     CliUtil.toolSetup();
/*  78 */     FixCalendarTZUtil util = null;
/*     */     try {
/*  80 */       util = new FixCalendarTZUtil();
/*     */     } catch (ServiceException e) {
/*  82 */       System.err.println(e.getMessage());
/*  83 */       System.exit(1);
/*     */     }
/*     */     try {
/*  86 */       CommandLine cl = util.getCommandLine(args);
/*  87 */       if (cl == null)
/*  88 */         return;
/*  89 */       if (!cl.hasOption("rulefile"))
/*  90 */         throw new ParseException("Missing required option --rulefile");
/*  91 */       String after = null;
/*  92 */       if (cl.hasOption("after")) {
/*  93 */         after = cl.getOptionValue("after");
/*     */       }
/*  95 */       util.doit(getZAuthToken(cl), cl.getOptionValue("rulefile"), cl.getOptionValues("a"), after, cl.hasOption("sync"));
/*  96 */       System.exit(0);
/*     */     } catch (ParseException e) {
/*  98 */       util.usage(e);
/*     */     } catch (Exception e) {
/* 100 */       System.err.println("Error occurred: " + e.getMessage());
/* 101 */       util.usage(null);
/*     */     }
/* 103 */     System.exit(1);
/*     */   }
/*     */   
/*     */   private static Element parseRuleFile(String ruleFilePath) throws IOException, ServiceException
/*     */   {
/* 108 */     File ruleFile = new File(ruleFilePath);
/* 109 */     if (!ruleFile.exists()) {
/* 110 */       throw new IOException("Missing rule file " + ruleFile.getAbsolutePath());
/*     */     }
/* 112 */     String docStr = new String(ByteUtil.getContent(ruleFile), "utf-8");
/* 113 */     Element tzfixupElem = Element.parseXML(docStr);
/* 114 */     tzfixupElem.detach();
/* 115 */     XmlFixupRules.parseTzFixup(tzfixupElem);
/* 116 */     return tzfixupElem;
/*     */   }
/*     */   
/*     */   private void doit(ZAuthToken zat, String ruleFilePath, String[] accts, String after, boolean sync) throws SoapFaultException, IOException, ServiceException
/*     */   {
/* 121 */     Element req = new Element.XMLElement(AdminConstants.FIX_CALENDAR_TZ_REQUEST);
/*     */     
/* 123 */     Element tzfixupElem = parseRuleFile(ruleFilePath);
/* 124 */     req.addElement(tzfixupElem);
/*     */     
/* 126 */     if ((accts == null) || (accts.length == 0))
/* 127 */       throw ServiceException.INVALID_REQUEST("Missing -a option", null);
/* 128 */     for (String acct : accts) {
/* 129 */       Element acctElem = req.addElement("account");
/* 130 */       acctElem.addAttribute("name", acct);
/*     */     }
/*     */     
/* 133 */     if (after != null) {
/* 134 */       Date afterTime = parseDatetime(after);
/* 135 */       if (afterTime != null) {
/* 136 */         SimpleDateFormat f = new SimpleDateFormat(CANONICAL_DATETIME_FORMAT);
/* 137 */         String tstamp = f.format(afterTime);
/* 138 */         System.out.printf("using cutoff time of %s\n", new Object[] { tstamp });
/* 139 */         req.addAttribute("after", afterTime.getTime());
/*     */       } else {
/* 141 */         System.err.printf("Invalid timestamp \"%s\" specified for --%s option\n", new Object[] { after, "after" });
/*     */         
/* 143 */         System.err.println();
/* 144 */         System.err.print(getAllowedDatetimeFormatsHelp());
/* 145 */         System.exit(1);
/*     */       }
/*     */     }
/*     */     
/* 149 */     if (sync) {
/* 150 */       req.addAttribute("sync", true);
/*     */     }
/* 152 */     auth(zat);
/* 153 */     getTransport().invokeWithoutSession(req);
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/mailbox/calendar/FixCalendarTZUtil.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */